package com.dstation.utils;
import com.dstation.domain.VideoInfo;
import java.util.ArrayList;
import java.util.List;

public class RecommentUtil {

    /**
     * 得到两个字符串的相似度
     * 参照算法实现：https://blog.csdn.net/ls386239766/article/details/38961745
     * @param str
     * @param target
     * @return
     */
    public static float getSimilarityRatio(String str, String target) {

        int d[][]; // 矩阵
        int n = str.length();
        int m = target.length();
        int i; // 遍历str的
        int j; // 遍历target的
        char ch1; // str的
        char ch2; // target的
        int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
        if (n == 0 || m == 0) {
            return 0;
        }
        d = new int[n + 1][m + 1];
        for (i = 0; i <= n; i++) { // 初始化第一列
            d[i][0] = i;
        }

        for (j = 0; j <= m; j++) { // 初始化第一行
            d[0][j] = j;
        }

        for (i = 1; i <= n; i++) { // 遍历str
            ch1 = str.charAt(i - 1);
            // 去匹配target
            for (j = 1; j <= m; j++) {
                ch2 = target.charAt(j - 1);
                if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
                    temp = 0;
                } else {
                    temp = 1;
                }
                // 左边+1,上边+1, 左上角+temp取最小
                d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
            }
        }

        return (1 - (float) d[n][m] / Math.max(str.length(), target.length())) * 100F;
    }

    /**
     * 根据用户是否登录推送视频
     * @param videoInfoList
     * @param isLogin
     * @return
     */
    public static List<VideoInfo> getRecommentVideos(List<VideoInfo> videoInfoList, boolean isLogin, VideoInfo currentVideo) {
        List<VideoInfo> recommentVideoList = new ArrayList<>();
        if(isLogin == false) {  //1. 若未登录，则最多推荐5个给用户
            for(int i = 0; i < videoInfoList.size() && i < 4; i ++) {   //直接拿到前4个给用户
                recommentVideoList.add(videoInfoList.get(i));
            }
        } else {    //2.若用户已登录，则根据算法推荐
            //2.1 声明分量所占权重weights：设置标题相似权重为0.4， 类别相似为0.4， 简介相似为0.2
            float sumSimilarity[] = new float[videoInfoList.size()];  //每个视频对应一个总的相似度
            float titleSimilarity[] = new float[videoInfoList.size()];  //每个视频对应一个标题相似度
            float classifyDescriptionSimilarity[] = new float[videoInfoList.size()];  //每个视频对应一个类别相似度
            float descriptionSimilarity[] =  new float[videoInfoList.size()];  //每个视频对应一个视频简介相似度
            for(int i = 0; i < videoInfoList.size(); i++) {
                titleSimilarity[i] = RecommentUtil.getSimilarityRatio(videoInfoList.get(i).getVideoTitle(), currentVideo.getVideoTitle());
                classifyDescriptionSimilarity[i] = RecommentUtil.getSimilarityRatio(videoInfoList.get(i).getVideoClassId().toString(),currentVideo.getVideoClassId().toString());
                descriptionSimilarity[i] = RecommentUtil.getSimilarityRatio(videoInfoList.get(i).getVideoDescription(), currentVideo.getVideoDescription());
                sumSimilarity[i] = (float) (0.4 * titleSimilarity[i] +  0.4 * classifyDescriptionSimilarity[i] + 0.2 * descriptionSimilarity[i]);
            }
            int indexArr[] = ArrayHelper.Arraysort(sumSimilarity, true);    //将数组排序后从大到小的排序
            for(int i = 0; i < videoInfoList.size() && i < 4; i ++) {
                recommentVideoList.add(videoInfoList.get(indexArr[i]));
            }
        }
        return recommentVideoList;
    }
}

